/*
 * 
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.tiberiumlabs.lailaps.list.storage;

import java.util.ArrayList;
import java.util.List;
import org.tiberiumlabs.lailaps.list.DownloadItem;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class ListHandler extends DefaultHandler implements ListAttributes {

    private ArrayList<DownloadItem> list = null;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("item".equals(qName)) {
            DownloadItem item = new DownloadItem(attributes.getValue(URL));
            item.setOutputFile(attributes.getValue(OUTPUT_FILE));
            String s = attributes.getValue(OUTPUT_PATH);
            if (s != null) {
                item.setOutputPath(s);
            }
            s = attributes.getValue(FULL_SIZE);
            if (s != null) {
                item.setFullSize(Long.parseLong(s));
            }
            s = attributes.getValue(COMPLETED_SIZE);
            if (s != null) {
                item.setCompletedSize(Long.valueOf(s));
            }
            s = attributes.getValue(FILE_CREATED);
            if (s != null) {
                // if file is not created - it is not appended to list file
                item.setTargetFileCreated(true);
            }
            s = attributes.getValue(STATUS);
            if (s != null) {
                // if it is not completed - it will not be appended to list file
                item.setStatus(COMPLETED);
            }
            list.add(item);
            return;
        }
        if ("list".equals(qName)) {
            int size = Integer.valueOf(attributes.getValue("size"));
            list = new ArrayList<DownloadItem>(size);
            return;
        }
    }

    public List<DownloadItem> getList() {
        return list;
    }

}
